%%%====================================================================
%%% File    : pipe_iter.erl
%%% Author  :  <vjache>
%%% Description: This is a simpliest pipe implementation. It may be 
%%%				 considered as a simple reference implementation of 
%%%				 a pipe callback module, and could be used for debug 
%%%				 purposes.
%%%
%%% Created : 24.03.2009
%%%====================================================================

-module(pipe_iter).

%%
%% Include files
%%
-include("../include/iter.hrl").
-include("../include/pipe.hrl").
-include("../include/util.hrl").
%%
%% Exported Functions
%%
-export([
		 new/1,
		 open/1,
		 push/2,
		 flush/1,
		 close/1,
		 test/1]).

%%====================================================================
%% API Functions
%%====================================================================

new(NextPipe) ->
	pipe:pipe_from_module(?MODULE, _Param=nil, NextPipe).

open({_Param,NextPipe}) -> %%PipeState
    pipe:open(NextPipe).
push(NextPipeState,?EOF) ->
	NextPipeState;
push(NextPipeState,Iter) when is_record(Iter, iter)->
	{IterState,NextPipeStateNew}=pipe:push_each(NextPipeState, Iter),
	ok=iter:close(IterState),
	NextPipeStateNew;
push(NextPipeState,Item) ->
	pipe:push(NextPipeState, Item).
flush(S) ->
	S.
close(NextPipeState) ->
	ok=pipe:close(NextPipeState).

%%====================================================================
%% Test Functions
%%====================================================================

test(L) ->
	S=pipe:open(new(pipe_console:new())),
	PipeState=pipe:push(S, iter:from_list(L)),
	pipe:close(PipeState).


%%====================================================================
%% Local Functions
%%====================================================================

